package nettcp2;

import java.io.*;
import java.net.Socket;

public class TCPClient {
	public static void main(String[] args) {
		Socket socket = null;
		OutputStream out = null;
		BufferedWriter writer = null;
		BufferedReader reader = null;
		try {
			// 创建套接字兑现，去请求服务器的ServerSocket
			socket = new Socket("192.168.56.1", 10000);
			out = socket.getOutputStream();
			writer = new BufferedWriter(new OutputStreamWriter(out));
			reader = new BufferedReader(new InputStreamReader(System.in));
			String line = null;				
			while ((line = reader.readLine())!=null) {
				writer.write(line);
				writer.newLine();  //需要新起来一行，不然无法将数据发出
				writer.flush();
				if ("exit".equals(line)) {
					break;
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (writer != null) {
					writer.close();
				}
				if (out != null) {
					out.close();
				}
				if (socket != null) {
					socket.close();
				}
				if (reader!=null) {
					reader.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
